package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.ParkingpassesModel;
import com.carpark.ejb.Model.ParkingspacesModel;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Sawior
 */
@Stateful
public class ParkingpassesDAOImpl implements ParkingpassesDAO {
    
    private EntityManager entityManager;
    
    public ParkingpassesDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }
    
    

    @Override
    public List<ParkingpassesModel> getAllParkingPasses() {
        return (List<ParkingpassesModel>) entityManager.createNamedQuery("ParkingpassesModel.findAll").getResultList();
    }

    @Override
    public int getParkingPassesSize() {
        return entityManager.createNamedQuery("ParkingpassesModel.findAll").getResultList().size();
    }

    @Override
    public ParkingpassesModel getParkingPassById(Integer id) {
        return (ParkingpassesModel) entityManager.createNamedQuery("ParkingpassesModel.findByParkingPassId").setParameter("parkingPassId", id).getSingleResult();
    }
    
    @Override
    public List<ParkingpassesModel> getParkingPassBySpaceId(ParkingspacesModel space) {
        return (List<ParkingpassesModel>) entityManager.createNamedQuery("ParkingpassesModel.findBySpace").setParameter("numberId", space).getResultList();
    }

    @Override
    public List<ParkingpassesModel> getParkingPassesByDateIssue(Date dateIssue) {
        return (List<ParkingpassesModel>) entityManager.createNamedQuery("ParkingpassesModel.findByDateIssue").setParameter("dateIssue", dateIssue).getResultList();
    }

    @Override
    public List<ParkingpassesModel> getParkingPassesByExpirationDate(Date expirationDate) {
        return (List<ParkingpassesModel>) entityManager.createNamedQuery("ParkingpassesModel.findByExpirationDate").setParameter("expirationDate", expirationDate).getResultList();
    }

    @Override
    public List<ParkingpassesModel> getParkingPassesByPassType(String passType) {
        return (List<ParkingpassesModel>) entityManager.createNamedQuery("ParkingpassesModel.findByPassType").setParameter("passType", passType).getResultList();
    } 
    
    void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public void addPass(ParkingpassesModel pass) {
        entityManager.getTransaction().begin();
        entityManager.persist(pass);
        entityManager.getTransaction().commit();
    }
    
    @Override
    public void updatePass(ParkingpassesModel pass) {
        entityManager.getTransaction().begin();
        entityManager.persist(pass);
        entityManager.flush();
        entityManager.refresh(pass);
        entityManager.getTransaction().commit();
    }

    @Override
    public void detelePass(ParkingpassesModel pass) {
        entityManager.getTransaction().begin();
       ParkingpassesModel toBeRemoved = entityManager.merge(pass);
  
        entityManager.remove(toBeRemoved);
        entityManager.getTransaction().commit();
    }

    @Override
    public int getByDateIssueSize(Date dateIssue) {
    List<ParkingpassesModel> list=(List<ParkingpassesModel>) entityManager.createNamedQuery("ParkingpassesModel.findByDateIssue").setParameter("dateIssue", dateIssue).getResultList();
    return list.size();
    }
    
}
